<?php
/**
 * Utilities class file
 * @package Utilities
 */

/**
 * This class contains a number of useful functions for a variety of projects.
 * 
 * @package Utilities
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */
class Utilities {
	
	/**
	 * Returns a proper English list from an array.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param array $array The array to implode
	 * @param string $glue The glue
	 * @param string $last The glue for the last item
	 * @return string The proper English list
	 */
	static public function implodeProper($array, $glue = ', ', $last_glue = ' and ') {
		$count = count($array);
		if (!is_array($array) or $count == 0) {
			return '';
		}
		else if ($count == 1) {
			return $array[0];
		}
		else if ($count == 2) {
			return implode($last_glue, $array);
		}
		else {
			$last = array_pop($array);
			return implode($glue, $array).trim($glue).$last_glue.$last;
		}
	}
	
	/**
	 * Implode an array with the keys.
	 * 
	 * @author adrian at foeder dot de <adrian@foeder.de>
	 * @param string $glue The glue
	 * @param array $array The array to implode
	 * @param string $valwrap The value to put around the values
	 * @return string The imploded array
	 */
	static public function implodeWithKeys($glue, $array, $valwrap='') {
		foreach($array as $key => $value) {
			$return[] = $key."=".$valwrap.$value.$valwrap;
		}
		return implode($glue, $return);
		//return (count($array) > 1) ? implode($glue, $return) : $return;
	}
	
	/**
	 * Finds the needle in a multi-dimensional haystack.
	 * from: http://us2.php.net/manual/en/function.array-search.php#73300
	 * 
	 * @author rob at robspages dot net <rob@robspages.net>
	 * @param string $needle The string to find
	 * @param array $haystack The array to search in
	 * @return array An array indicating the position of the needle
	 */
	static public function multiArraySearch($needle, $haystack){
		$value = false;
		$x = 0;
		foreach($haystack as $temp){
			$search = array_search($needle, $temp);
			if (strlen($search) > 0 && $search >= 0){
				$value[0] = $x;
				$value[1] = $search;
			}
			$x++;
		}
		return $value;
	}
	
	/**
	 * Rounds a number to the nearest X.
	 * from: http://marc.info/?l=php-general&m=111478099711526&w=2
	 * 
	 * @author Chris Boget <chris.boget@wild.net>
	 * @param int $number The number to round
	 * @param int $nearest The nearest number to round to
	 */
	static public function roundToNearest($number, $nearest = 5) {
		$rounded = 0;
		$mod = $number % $nearest;
		
		if ($mod >= 0) {
			$rounded = ($mod > ($nearest / 2)) ? $number + ($nearest - $mod) : $number - $mod;
		}
		else {
			$rounded = ($mod > (-$nearest / 2)) ? $number - $mod : $number + (-$nearest - $mod);
		}
		
		return $rounded;
	}
	
	/**
	 * Sanitizes information to be put in the database.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param string $value The value to clean
	 * @return string The SQL-safe value
	 */
	static public function sqlClean($value) {
		if (get_magic_quotes_gpc()) {
			if (ini_get('magic_quotes_sybase')) {
				$value = str_replace("''", "'", $value);
			}
			else {
				$value = stripslashes($value);
			}
		}
		$value = mysql_real_escape_string($value);
		return htmlentities($value);
	}
	
	/**
	 * Sanitizes an entire array.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @param array $array The array to sanitize
	 * @return array The SQL-safe array
	 */
	static public function sqlCleanArray($array) {
		$cleaned = array();
		foreach ($array as $key => $item) {
			$key = Utilities::sqlClean($key);
			$item = Utilities::sqlClean($item);
			$cleaned[$key] = $item;
		}
		return $cleaned;
	}
	
}

?>